package fr.afcepf.atod16.groupe1.projet1.entities.catalogue;

import fr.afcepf.atod16.groupe1.projet1.entities.Entite;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;

/**
 * L'entité produit
 * @author cedric
 */
@Entity
@Table(name = "produits")
public class Produit implements Serializable, Entite {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    private Integer id;
    @Column(name="code_ean13", nullable = false, length = 13, unique = true)
    private String codeEAN13;
    @Column(name="date_creation", nullable = false)
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date dateCreation;
    @Column(name="stock", nullable = false)
    private Integer stock;
    @Column(name="prix", nullable = false)
    private Integer prix;
    @ManyToOne(optional = false)
    @JoinColumn(name = "tva_id", nullable=false)
    private TVA tva;
    @OneToMany
    @JoinColumn(name = "produit_id", nullable=false)
    private List<FicheProduit> fiches;
    @ManyToMany
    @JoinTable(name = "promos_produits",
    joinColumns = {
        @JoinColumn(name = "id_produit")},
    inverseJoinColumns = {
        @JoinColumn(name = "id_promo")})
    private List<Promo> promos;

    public Produit() {
    }

    public Produit(Integer id, String codeEAN13, Date date, Integer stock,
            Integer prix, TVA tva, List<FicheProduit> fiches, List<Promo> promos) {
        this.id = id;
        this.codeEAN13 = codeEAN13;
        this.dateCreation = date;
        this.stock = stock;
        this.prix = prix;
        this.tva = tva;
        this.fiches = fiches;
        this.promos = promos;
    }

    @Override
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getCodeEAN13() {
        return codeEAN13;
    }

    public void setCodeEAN13(String codeEAN13) {
        this.codeEAN13 = codeEAN13;
    }

    public Date getDateCreation() {
        return dateCreation;
    }

    public void setDateCreation(Date dateCreation) {
        this.dateCreation = dateCreation;
    }

    public Integer getStock() {
        return stock;
    }

    public void setStock(Integer stock) {
        this.stock = stock;
    }

    public Integer getPrix() {
        return prix;
    }

    public void setPrix(Integer prix) {
        this.prix = prix;
    }

    public TVA getTva() {
        return tva;
    }

    public void setTva(TVA tva) {
        this.tva = tva;
    }

    public List<FicheProduit> getFiches() {
        return fiches;
    }

    public void setFiches(List<FicheProduit> fiches) {
        this.fiches = fiches;
    }

    public List<Promo> getPromos() {
        return promos;
    }

    public void setPromos(List<Promo> promos) {
        this.promos = promos;
    }

    @Override
    public String toString() {
        return "Produit [id=" + id + ", codeEAN13=" + codeEAN13 + ", date="
                + dateCreation + ", stock=" + stock + ", prix=" + prix + ", tva=" + tva
                + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((codeEAN13 == null) ? 0 : codeEAN13.hashCode());
        result = prime * result + ((dateCreation == null) ? 0 : dateCreation.hashCode());
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((prix == null) ? 0 : prix.hashCode());
        result = prime * result + ((stock == null) ? 0 : stock.hashCode());
        result = prime * result + ((tva == null) ? 0 : tva.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        Produit other = (Produit) obj;
        if (codeEAN13 == null) {
            if (other.codeEAN13 != null) {
                return false;
            }
        } else if (!codeEAN13.equals(other.codeEAN13)) {
            return false;
        }
        if (dateCreation == null) {
            if (other.dateCreation != null) {
                return false;
            }
        } else if (!dateCreation.equals(other.dateCreation)) {
            return false;
        }
        if (id == null) {
            if (other.id != null) {
                return false;
            }
        } else if (!id.equals(other.id)) {
            return false;
        }
        if (prix == null) {
            if (other.prix != null) {
                return false;
            }
        } else if (!prix.equals(other.prix)) {
            return false;
        }
        if (stock == null) {
            if (other.stock != null) {
                return false;
            }
        } else if (!stock.equals(other.stock)) {
            return false;
        }
        if (tva == null) {
            if (other.tva != null) {
                return false;
            }
        } else if (!tva.equals(other.tva)) {
            return false;
        }
        return true;
    }
}
